package com.zjeav.lingjiao.ui.presenter;

import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.listener.CommonListener;
import com.zjeav.lingjiao.base.request.GetVersionRequest;
import com.zjeav.lingjiao.base.request.OtherLoginRequest;
import com.zjeav.lingjiao.base.request.SuggestRequest;
import com.zjeav.lingjiao.base.response.VersionResponse;
import com.zjeav.lingjiao.base.response.WXName;
import com.zjeav.lingjiao.base.service.BindWeChatService;
import com.zjeav.lingjiao.base.service.SuggestService;
import com.zjeav.lingjiao.base.service.VersionService;
import com.zjeav.lingjiao.base.tools.ObservableServiceTool;
import com.zjeav.lingjiao.base.tools.RetrofitInstance;
import com.zjeav.lingjiao.ui.personCenter.view.SysContract;

import io.reactivex.Observable;
import retrofit2.Retrofit;

public class SysPresenter implements CommonListener {
    private SysContract.View view;
    private SysContract.FeedBackView feedBackView;

    public SysPresenter(SysContract.FeedBackView feedBackView) {
        this.feedBackView = feedBackView;
    }

    public SysPresenter(SysContract.View view) {
        this.view = view;
    }

    public void getVersion(GetVersionRequest request) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        VersionService service = retrofit.create(VersionService.class);
        ObservableServiceTool.getResult((Observable) service.getVersion(request), 0, this);
    }
    public void feedback(SuggestRequest request){
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        SuggestService service = retrofit.create(SuggestService.class);
        ObservableServiceTool.getResult((Observable) service.submmitSuggest(request), 1, this);
    }
    public void bindWechat(OtherLoginRequest request){
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        BindWeChatService service = retrofit.create(BindWeChatService.class);
        ObservableServiceTool.getResult((Observable) service.bindWechat(request), 2, this);
    }

    @Override
    public void OnSuccess(Result result, int type) {
        if (type == 0) {
            this.view.getVersion((VersionResponse) result.getData());
        }else if(type==1){
            if (result.getCode()==0){
                this.feedBackView.FeedBackSuccess(result);
            }else {
                this.feedBackView.showError(null,result.getMessage());
            }

        }else if (type==2){
            if (result.getCode()==0){
                this.view.bindWeChat((WXName) result.getData());
            }else {
                this.view.showError(null,result.getMessage());
            }
        }
    }

    @Override
    public void OnFailure(Throwable throwable, int type) {
        if (type == 0) {
            this.view.showError(throwable,"");
        }else if (type==1){
            this.feedBackView.showError(throwable,"");
        }else if (type==2){
            this.view.showError(throwable,"");
        }
    }
}
